package com.example.demo.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;


/**
 * @author YanKuo
 * @date 2019-06-22
 */
@Data
public class ResultJson<T> {
    //正常200,-1:无内容, ,-100登录，-200正常失败
    private Integer code;
    //0:成功，-1 失败异常
    private Integer status;
   //提示信息
    private String msg;
    //结果json
    private T data;

    public ResultJson (){
        this.code = 200;
        this.status = 0;
        this.msg = "成功";
        this.data = (T) "";
    }
    public ResultJson setCode(Integer code) {
        this.code = code;
        return this;
    }
    public ResultJson setStatus(Integer status) {
        this.status = status;
        return this;
    }
    public ResultJson setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public ResultJson setData(T data) {
        this.data = data;
        return this;
    }
//    //session过期
//    public Boolean getSessionCode(){
//        Boolean sessionStatus = true;
//        Subject currentSession = SecurityUtils.getSubject();
//        if (currentSession.getPrincipal()!=null){
//            sessionStatus = false;
//        }
//        return  sessionStatus;
//    }

    /**
     * @Author zhangjunk
     * @Description //定义异常返回结果
     * @Date 14:23 2020/5/29
     * @Param [msg]
     * @return enn.cn.EnnHrssc.base.ResultJson
     **/
//    public static ResultJson error(BaseErrorInfoInterface errorInfo) {
//        ResultJson json = new ResultJson();
//        json.setCode(Integer.valueOf(errorInfo.getResultCode()));
//        json.setMsg(errorInfo.getResultMsg());
//        json.setData(null);
//        return json;
//    }
    /**
     * @Author zhangjunk
     * @Description //定义异常返回结果
     * @Date 14:23 2020/5/29
     * @Param [msg]
     * @return enn.cn.EnnHrssc.base.ResultJson
     **/
    public static ResultJson error(Integer code, String msg) {
        ResultJson json = new ResultJson();
        json.setCode(code);
        json.setMsg(msg);
        json.setData(null);
        return json;
    }

    /**
     * @Author zhangjunk
     * @Description //定义异常返回结果
     * @Date 14:23 2020/5/29
     * @Param [msg]
     * @return enn.cn.EnnHrssc.base.ResultJson
     **/
    public static ResultJson error( String msg) {
        ResultJson json = new ResultJson();
        json.setCode(-1);
        json.setMsg(msg);
        json.setData(null);
        return json;
    }


    /**
     *
     * @param code
     * @param status
     * @param msg
     * @return
     */
    public static ResultJson error(Integer code,Integer status, String msg) {
        ResultJson json = new ResultJson();
        json.setCode(code);
        json.setMsg(msg);
        json.setStatus(status);
        json.setData(null);
        return json;
    }

    /**
     * @Author wangyubq
     * @Description 请求成功返回结果
     * @Date 14:23 2020/6/17
     * @Param [data]
     * @return enn.cn.EnnHrssc.base.ResultJson
     **/
    public static ResultJson success( Object data) {
        ResultJson json = new ResultJson();
        json.setData(data);
        return json;
    }

    /**
     * @Author wangyubq
     * @Description 请求成功返回结果
     * @Date 14:23 2020/6/17
     * @return enn.cn.EnnHrssc.base.ResultJson
     **/
    public static ResultJson success() {
        ResultJson json = new ResultJson();
        return json;
    }

    //转换成json类型
    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }
}

